package net.mikoo.seals.manage.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.mikoo.seals.biz.service.OrganizationService;
import net.mikoo.seals.biz.service.PartyServices;
import net.mikoo.seals.common.WebContext;
import net.mikoo.seals.common.exception.ParametersException;
import net.mikoo.seals.common.model.PageModel;
import net.mikoo.seals.manage.web.common.BaseController;
import net.mikoo.seals.model.Collections;
import net.mikoo.seals.model.Organization;
import net.mikoo.seals.model.OrganizationParty;
import net.mikoo.seals.model.OrganizationQuery;
import net.mikoo.seals.model.Party;
import net.mikoo.seals.model.PartyQuery;

@Controller
@RequestMapping("/org")
public class OrganizationController extends BaseController {

	private final static Logger logger = LoggerFactory.getLogger(OrganizationController.class);
	
	@Autowired
	private OrganizationService organizationService;
	
	@Autowired
	private PartyServices partyServices;
	
	
	
	
	
	@RequestMapping("/test")
	public String test(OrganizationQuery query,Model model, HttpServletRequest request){
		
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		List<Organization> orgs = organizationService.getListByQuery(query);
		model.addAttribute("orgs", orgs);
		return "org/left2";
	}
	
	
	
	
	@RequestMapping("/add")
	@ResponseBody Map<String, Object> add(Organization org, HttpServletRequest request){
		try {
			
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			org.setCompanyId(companyId);
			
			organizationService.create(org);
			
			Map<String,Object> data = new HashMap<>();
			data.put("id", org.getOrganizationId());
			data.put("pid", org.getParentId());
			data.put("name", org.getName());
			
			return result(SUCCESS, "添加成功",data);
		}catch(ParametersException pe) {
			logger.error("", pe);
			return result(ERROR, pe.getMessage());
		}catch(Exception e) {
			logger.error("系统错误", e);
			return result(ERROR, MSG_SYSTEM_ERROR);
		}
	}
	
	@RequestMapping("/modify")
	@ResponseBody Map<String, Object> modify(Organization org, HttpServletRequest request){
		try {
			
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			org.setCompanyId(companyId);
			organizationService.modify(org);
			return result(SUCCESS, "修改成功");
		}catch(ParametersException pe) {
			logger.error("", pe);
			return result(ERROR, pe.getMessage());
		}catch(Exception e) {
			logger.error("系统错误", e);
			return result(ERROR, MSG_SYSTEM_ERROR);
		}
	}
	
	@RequestMapping("/delete")
	@ResponseBody Map<String, Object> delete(OrganizationQuery query, HttpServletRequest request){
		try {
			
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			query.setCompanyId(companyId);
			organizationService.deleteByQuery(query);
			return result(SUCCESS, "删除成功");
		}catch(ParametersException e) {
			logger.error("", e);
			return result(ERROR, e.getMessage());
		}catch(Exception e) {
			logger.error("系统错误", e);
			return result(ERROR, MSG_SYSTEM_ERROR);
		}
	}
	@RequestMapping("/move")
	@ResponseBody Map<String, Object> move(Organization org, HttpServletRequest request){
		try {
			
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			org.setCompanyId(companyId);
			organizationService.modify(org);
			return result(SUCCESS, "移动成功");
		}catch(ParametersException e) {
			logger.error("", e);
			return result(ERROR, e.getMessage());
		}catch(Exception e) {
			logger.error("系统错误", e);
			return result(ERROR, MSG_SYSTEM_ERROR);
		}
	}
	
	
	
	
	
	
	
	
	
}
